/*NOTE: This is model code to illustrate the logic used to generate the datasets for analysis in the SCARP pilot.
This code is not intended to run as is - due to Epic restrictions on the sharing of intellectual property all 
of the table and attribute names have been "masked" with names enclosed in square brackets if they were pulled 
from Clarity (EPIC).*/

/*****************************************************
PROJECT: BRIDGE C2 
PILOT: SCRAP
DATE: 8/4/2022
7/23/2023 corrected coding for orders within 1 year of due
corrected filtering for patients with 1 year observation after first due date

PROJECT DESCRIPTION: Use ASCEND approach to compare CRC, CVC and 
BC cancer prevention services in patients with
	No SDH Screening
	Documented Need
	Documented No Need
Stratified by SDH need: Food, Housing, Transportation
STUDY PERIOD: 7/1/2016-2/29/2020		

DETAILS (this file): 
CRC Outcome variables by SDH
	UTD at index 
	Type of last screen prior to index: None, FIT/FOBT, Imaging
	Months UTD over observation period
	Among Patients due 12 months before obs end, Receipt of screening order within 12 months
	Among patients with order, 
			receipt of order for imaging 
			receipt of screen while due(Poisson with months after due as offset)
		  	Receipt of FIT/FOBT order
			Receipt of Imaging/Order
Table 2.Columns Food Insecurity: Need/ No Need/ Not documented
Table 3 Columns Housing Insecurity: Need/ No Need/ Not documented
Table 4 Columns Transportation Insecurity: Need/ No Need/ Not documented
		

INPUT DATA:
SCRAP.CVC_outcomes;

OUTPUT DATA: excek fukes cirrected:
SCRAP.CVC_Table2_Food
SCRAP.CVC_Table3_Housing
SCRAP.CVC_Table4_Transportation


SECTIONS:
A. Dynamic variables and analysis file
B. Full cohort variables-this section was not rerun 7/26/2023
	B1. UTD at index 
	B2. Imaging screen prior to index: 
	B3. Months UTD over observation period
	B4. Visits in year following Index (includes index visit

C.Among Patients due 12 months before obs end > filtering on this section corrected 
	C1.Receipt of any screening order within 12 months

D.Among patients with order, 
	D1. receipt of screen while due(Poisson with months after due as offset)


****************************************************/
options compress=yes reuse=yes spool;
libname SCRAP "e:\\sasroot\BRIDGE C2\SCRAP\";
libname RD odbc noprompt="dsn=research_dev;Trusted_Connection=yes" bulkload=yes;
options mprint symbolgen;
/****************************************************/

/***************************************************
A. Dynamic variables and analysis file
***************************************************/
%let cancer=CVC;
%Let covariates=female race_ethnic2 language_c  FPL_cat2 age_cat payor_type_research visit_cat;
%Let class=delivery_site_id female age_cat race_ethnic2 language_c  FPL_cat2 visit_cat payor_type_research;

data scrap.&cancer._analysis;
set SCRAP.&cancer._analysis;
sdh_trans=sdh_transportation;
if one_year_due<=observation_end and one_year_due^=. then do;
	order_1y_corrected=order_date^=. and order_date<one_year_due;
	screen_1yr_corrected=first_screen_date^=. and first_screen_date<one_year_due;
end;
run;

proc freq data=scrap.&cancer._analysis;
where one_year_due<=observation_end and one_year_due^=.;
tables order_date*one_year_due/missing list;
run;

proc freq data=scrap.CVC_analysis;
tables order_1y_corrected*order_1y/missing;
run;

data analysis;
set SCRAP.&cancer._analysis;
run;

proc sort data=analysis;
by delivery_site_id;
run;


/********************************************************
/*C.Among Patients due 12 months before obs end, */
*********************************************************/;
data analysis2;
set analysis;
where one_year_due^=. and observation_end>=one_year_due;
run;

%let SDH=SDH_Food; 
%let SDH=SDH_Housing;
%Let SDH=SDH_Trans;

/*	C1.Receipt of any screening order within 12 months*/
proc genmod data=analysis2 descending;
class &class &sdh /ref=first;
model order_1y_corrected=&sdh &covariates/link=log d=binomial type3;
repeated subject=delivery_site_id/type=exch;
LSmeans &sdh/ilink CL diff OM;
ods output LSmeans=order_&sdh lsmestimates=order_diffs_&sdh;
lsmestimate &sdh '1 vs 2' -1 0 1/cl  om ilink;
lsmestimate &sdh '3 vs 2' -1 1 0 /cl   om ilink;
run;


/******************************************
/*D.Among patients with order, */
/******************************************/;
	data analysis3;
	set analysis2;
	where order_1y_corrected=1;
	run;

/*	D2. receipt of any screen*/
/*	proc genmod data=analysis3 descending;*/
/*	class &class &sdh /ref=first;*/
/*	model screen_1yr_corrected=&sdh &covariates/link=log d=binomial type3;*/
/*	repeated subject=delivery_site_id/type=exch;*/
/*	LSmeans &sdh/ilink CL OM;*/
/*	ods output LSmeans=screen_&sdh lsmestimates=screen_diffs_&sdh;*/
/*	lsmestimate &sdh '1 vs 2' -1 0 1/cl  om ilink;*/
/*	lsmestimate &sdh '3 vs 2' -1 1 0 /cl   om ilink;*/
/*	run;*/


proc freq data=scrap.cvc_analysis;
where one_year_due^=. and observation_end>=one_year_due;
tables SDH_food--sdh_transportation;
run;

proc freq data=scrap.cvc_analysis;
where one_year_due^=. and observation_end>=one_year_due
and order_1y_corrected=1;
tables SDH_food--sdh_transportation;
run;


proc freq data=scrap.cvc_analysis;
where one_year_due^=. and observation_end>=one_year_due
and order_1y_corrected=1;
tables (SDH_food--sdh_transportation)*screen_1yr_corrected/missing;
run;